package cn.stylefeng.guns.modular.rw.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.core.ParamUtil;
import cn.stylefeng.guns.modular.rw.entity.Videos;
import cn.stylefeng.guns.modular.rw.service.IVideosService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 描述：视频管理表 controller
 * 作者：chenhuiyan
 * 时间：2020-04-30
 */
@Controller
@RequestMapping("/rw/videos")
public class VideosController extends BaseController {

    private String PREFIX = "/modular/rw/videos/";

    @Autowired
    private IVideosService videosService;

    /**
     * 跳转到管理首页
     */
    @RequestMapping(value = "")
    public String index() {
        return PREFIX + "videos.html";
    }

    /**
     * 获取列表
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Object list() {
        Map<String, Object> params = ParamUtil.getParams();
        Page<Map<String, Object>> list = videosService.list(params);
        return LayuiPageFactory.createPageInfo(list);
    }

    /**
     * 跳转添加/修改页面
     * @return
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit() {
        return PREFIX + "videos_add.html";
    }

    /**
     * 上传视频
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadVideos")
    @ResponseBody
    public ResponseData uploadVideos(MultipartFile file){
        // 非空判断
        if (file.isEmpty()) {
            throw new RequestEmptyException("请求参数错误！");
        }
        videosService.uploadVideos(file);

        return SUCCESS_TIP;
    }

    /**
     * 跳转选择视频页面
     * @return
     */
    @RequestMapping(value = "/videosIndex")
    public String videosIndex() {
        return PREFIX + "videosIndex.html";
    }

    /**
     * 获取详情
     * @param videosId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInfoById/{videosId}")
    public Object getInfoById(@PathVariable("videosId") String videosId){
        Videos videos = videosService.getById(videosId);
        return videos;
    }

    /**
     * 删除图片（物理删除）
     * @param videosId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public ResponseData delete(@RequestParam(name = "videosId") String videosId){
        if(ToolUtil.isEmpty(videosId)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        videosService.onDelete(videosId);
        return SUCCESS_TIP;
    }

}
